package com.example.observer.config;

import java.util.HashMap;
import java.util.Map;

import com.clickhouse.client.ClickHouseClient;
import com.clickhouse.client.ClickHouseConfig;
import com.clickhouse.client.ClickHouseNodeSelector;
import com.clickhouse.client.ClickHouseNodes;
import com.clickhouse.client.ClickHouseProtocol;
import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.data.ClickHouseCompression;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ClickHouseConfiguration {
    @Bean
    ClickHouseNodes clickHouseNodes(
            @Value("${ob.clickhouse.host:}") String host) {
        return ClickHouseNodes.of(host);
    }

    @Bean
    ClickHouseClient clickHouseClient() {
        var options = Map.of(
                ClickHouseClientOption.COMPRESS_ALGORITHM, ClickHouseCompression.GZIP,
                ClickHouseClientOption.DECOMPRESS_ALGORITHM, ClickHouseCompression.GZIP,
                ClickHouseClientOption.DATABASE, "otel");
        var config = new ClickHouseConfig(new HashMap<>(options));
        return ClickHouseClient.builder().config(config).build();
    }
}
